package com.zygk.library.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.AbsoluteLayout;
import android.widget.ProgressBar;
import com.yanzhenjie.nohttp.NoHttp;
import com.zygk.library.R;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import wendu.dsbridge.DWebView;

/**
 * 带进度条的WebView
 */
@SuppressWarnings("deprecation") public class ProgressDWebView extends DWebView {

  private ProgressBar progressbar;

  public ProgressDWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
    progressbar.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, 5, 0, 0));
    addView(progressbar);
    // setWebViewClient(new WebViewClient(){});
    setWebChromeClient(new WebChromeClient());
  }

  public class WebChromeClient extends android.webkit.WebChromeClient {
    @Override public void onProgressChanged(WebView view, int newProgress) {
      if (newProgress == 100) {
        progressbar.setVisibility(GONE);
      } else {
        if (progressbar.getVisibility() == GONE) progressbar.setVisibility(VISIBLE);
        progressbar.setProgress(newProgress);
      }
      super.onProgressChanged(view, newProgress);
    }
  }

  @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) progressbar.getLayoutParams();
    lp.x = l;
    lp.y = t;
    progressbar.setLayoutParams(lp);
    super.onScrollChanged(l, t, oldl, oldt);
  }

  // 这个是webview截取滑动
  //@Override
  //public boolean onTouchEvent(MotionEvent event) {
  //  requestDisallowInterceptTouchEvent(true);
  //  return super.onTouchEvent(event);
  //}

  // cookie同步到WebView。
  @Override public void loadUrl(String url, Map<String, String> httpHeader) {
    if (httpHeader == null) {
      httpHeader = new HashMap<>();
    }

    // 这里你还可以添加一些自定头。
    httpHeader.put("AppVersion", "1.0.0"); // 比如添加app版本信息，当然实际开发中要自动获取哦。

    URI uri = null;
    try {
      uri = new URI(url);
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
    if (uri != null) {
      java.net.CookieStore cookieStore = NoHttp.getCookieManager().getCookieStore();
      List<HttpCookie> cookies = cookieStore.get(uri);

      // 同步到WebView。
      android.webkit.CookieManager webCookieManager = android.webkit.CookieManager.getInstance();
      webCookieManager.setAcceptCookie(true);
      for (HttpCookie cookie : cookies) {
        String cookieUrl = cookie.getDomain();
        String cookieValue = cookie.getName() + "=" + cookie.getValue()
            + "; path=" + cookie.getPath()
            + "; domain=" + cookie.getDomain();

        webCookieManager.setCookie(cookieUrl, cookieValue);
      }

      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        webCookieManager.flush();
      } else {
        android.webkit.CookieSyncManager.createInstance(NoHttp.getContext()).sync();
      }
    }
    super.loadUrl(url, httpHeader);
  }
}